package gameHandling;

import gameObject.Fence;

import javax.microedition.lcdui.Graphics;

import main.PlayingCanvas;

public class PlaceFenceHandling extends PlaceHandling {
	Fence fence;
	int[][] map = new int[8][20];
	static int[] dirI = new int[] { 0, -1, 1, 0, 1, -1, -1, 1 };
	static int[] dirJ = new int[] { 1, 0, 0, -1, 1, 1, -1, -1 };
	boolean isDone = false;
	public PlaceFenceHandling() {
		fence = new Fence();
		int rows = PlayingCanvas.getGame().tiledMap.getRows();
		int cols = PlayingCanvas.getGame().tiledMap.getColumns();
		for (int i = 0; i < cols; i++) {
			map[i][0] = 1;
			map[i][rows - 1] = 1;
			for (int j = 1; j < rows - 1; j++) {
				if (PlayingCanvas.getGame().objectMap[i][j] == 2) {
					map[i][j] = 1;
				} else {
					map[i][j] = 0;
				}
			}
		}
		for (int j = 0; j < rows; j++) {
			if (map[0][j] == 1) {
				PathFinder.getFenceConnect(0, j, map, 9);
			}
			if (map[cols - 1][j] == 1) {
				PathFinder.getFenceConnect(cols - 1, j, map, -9);
			}
		}
		for (int i = 0; i < cols; i++) {
			for (int j = 0; j < rows; j++) {
				if (map[i][j] == 0) {
					int l = 0;
					int r = 0;
					for (int k = 0; k < 8; k++) {
						int ni = i + dirI[k];
						int nj = j + dirJ[k];
						if (ni < 0) {
							l++;
						} else if (ni == cols) {
							r++;
						} else if (map[ni][nj] == 9) {
							l++;
						} else if (map[ni][nj] == -9) {
							r++;
						}
					}
					map[i][j] = r * l;
				}
			}
		}
		for (int i = 0; i < cols; i++) {
			for (int j = 1; j < rows - 1; j++) {
				if (map[i][j] ==0 && PlayingCanvas.getGame().objectMap[i][j] > 0) {
					map[i][j] = 999;
				}
			}
		}
	}

	public boolean pointerTaped(int x, int y) {
		
		int i = x / 30;
		int j = y / 30;
		
		if (j > 0 && j < 19 && map[i][j] == 0) {
			if(i == colIndex && j == rowIndex){
				execute();
				return true;
			}
			colIndex = i;
			rowIndex = j;
			PlayingCanvas.getInstance().btnPutFence.setVisible(true);
		}
		return true;
	}

	public void paint(Graphics g) {
		// super.paint(g);
		
		int rows = PlayingCanvas.getGame().tiledMap.getRows();
		int cols = PlayingCanvas.getGame().tiledMap.getColumns();
		int w = PlayingCanvas.getGame().tiledMap.getCellWidth();
		int h = PlayingCanvas.getGame().tiledMap.getCellHeight();
		int y = PlayingCanvas.getGame().getY();

		g.setColor(0x880000);
		for (int i = 0; i < cols; i++) {
			for (int j = 0; j < rows; j++) {
				if (map[i][j] != 0) {
					g.drawRect(i * w + 1, j * h + y + 1, w - 3, h - 3);
				}
			}
		}
		if (rowIndex != -1) {
			g.setColor(0x00ff00);
			g.drawRect(colIndex * w + 1, rowIndex * h + y + 1, w - 3, h - 3);
			if(!isDone){
				fence.getSprite().setPosition(colIndex * 30, rowIndex * 30 + y);
				fence.getSprite().paint(g);
			}
		}
	}

	public void execute() {
		
		if(PlayingCanvas.getGame().money<10){
			PlayingCanvas.getInstance().setMessage("Not enough money");
			return;
		}
		PlayingCanvas.getGame().money-=10;
		PlayingCanvas.getInstance().setMessage("-10G");
		PlayingCanvas.getInstance().doneHandling();
		
		PlayingCanvas.getGame().AddFence(fence, colIndex, rowIndex);
		isDone = true;
		
	}
}
